package require Tk
package require tcl3d 0.5.0
set location1 [pwd]
set vdist       30
set xRotate     0.0
set yRotate     0.0
set zRotate     0.0
set animStarted 0
set frameCount  0
set buildInfo   "Info"
set appName     "Visual SurfPB"
set globalflag 0
set coor_type 1
set skipInitData 1
set skipData 1
set threeTrajs no
set molArray11 1
set molArray12 0
set molArray13 0
set molArray14 0
set molArray21 0
set molArray22 1
set molArray23 0
set molArray24 0
set state11 active
set state21 disabled
set continuumModel PBSA
set gridInterval 0.5
set probeRad 1.4
set saltCon 0.1
set temperature 298.15
set epsIn 1
set epsOut 80
set pb 1
set nonpolar 1
set entropy 1
set gb 1
set opt 1
set topFileName ""
set top "      "
set topShortFileName top
set trajFilesName ""
set traj "     "
set trajShortFilesName traj
set liFilesName "liFilesName"
set re " "
set reShortFilesName re
set reFilesName "reFilesName"
set li " "
set liShortFilesName li
set trajflag1 0
set trajflag2 0
set mol1 1
set mol2 2
set fontSize 40
proc trans { w args } {
    $w postredisplay
}
proc rotX { w angle } {
    set ::xRotate [expr $::xRotate + $angle]
    if { $::xRotate < 0.0 } {
        set ::xRotate [expr $::xRotate + 360.0]
    } elseif { $::xRotate > 360.0 } {
        set ::xRotate [expr $::xRotate - 360.0]
    }
    $w postredisplay
}

proc rotY { w angle } {
    set ::yRotate [expr $::yRotate + $angle]
    if { $::yRotate < 0.0 } {
        set ::yRotate [expr $::yRotate + 360.0]
    } elseif { $::yRotate > 360.0 } {
        set ::yRotate [expr $::yRotate - 360.0]
    }
    $w postredisplay
}



proc rotZ { w angle } {
    set ::zRotate [expr $::zRotate + $angle]
    if { $::zRotate < 0.0 } {
        set ::zRotate [expr $::zRotate + 360.0]
    } elseif { $::zRotate > 360.0 } {
        set ::zRotate [expr $::zRotate - 360.0]
    }    
    $w postredisplay
}

proc handleRot {x y win} {
    global cx cy
    rotY $win [expr {180 * (double($x - $cx) / [winfo width $win])}]
    rotX $win [expr {180 * (double($y - $cy) / [winfo height $win])}]
    set cx $x
    set cy $y
}

proc DrawTetra {w} {
   
    glEnable GL_LIGHTING
#glDisable GL_LIGHTING
    glShadeModel GL_SMOOTH
    glEnable GL_CULL_FACE
    glBegin GL_TRIANGLES
    glEnable GL_BLEND
    glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA
    glBlendEquation GL_FUNC_ADD
    glEnable GL_POINT_SMOOTH
    glEnable GL_LINE_SMOOTH
    glEnable GL_POLYGON_SMOOTH

    array set infoAll [Read_data]
    set points $infoAll(-1,0)
    set triangles $infoAll(-1,1)
    set flag $infoAll(-1,2)
        for {set i $points} {$i < [expr $points + $triangles]} {incr i} {
            if {$flag == 1} {
            glColor3f $infoAll($infoAll($i,1),6) $infoAll($infoAll($i,1),7) $infoAll($infoAll($i,1),8) 
            glNormal3f $infoAll($infoAll($i,1),3) $infoAll($infoAll($i,1),4) $infoAll($infoAll($i,1),5)           
            glVertex3f  $infoAll($infoAll($i,1),0) $infoAll($infoAll($i,1),1) $infoAll($infoAll($i,1),2)               
            glColor3f $infoAll($infoAll($i,2),6) $infoAll($infoAll($i,2),7) $infoAll($infoAll($i,2),8)   
            glNormal3f $infoAll($infoAll($i,2),3) $infoAll($infoAll($i,2),4) $infoAll($infoAll($i,2),5)             
            glVertex3f $infoAll($infoAll($i,2),0) $infoAll($infoAll($i,2),1) $infoAll($infoAll($i,2),2)             
            glColor3f $infoAll($infoAll($i,3),6) $infoAll($infoAll($i,3),7) $infoAll($infoAll($i,3),8)  
            glNormal3f $infoAll($infoAll($i,3),3) $infoAll($infoAll($i,3),4) $infoAll($infoAll($i,3),5)          
            glVertex3f  $infoAll($infoAll($i,3),0) $infoAll($infoAll($i,3),1) $infoAll($infoAll($i,3),2)    
            } else {
            glColor3f $infoAll($infoAll($i,1),6) $infoAll($infoAll($i,1),7) $infoAll($infoAll($i,1),8)       
            glVertex3f  $infoAll($infoAll($i,1),0) $infoAll($infoAll($i,1),1) $infoAll($infoAll($i,1),2)               
            glColor3f $infoAll($infoAll($i,2),6) $infoAll($infoAll($i,2),7) $infoAll($infoAll($i,2),8)              
            glVertex3f $infoAll($infoAll($i,2),0) $infoAll($infoAll($i,2),1) $infoAll($infoAll($i,2),2)             
            glColor3f $infoAll($infoAll($i,3),6) $infoAll($infoAll($i,3),7) $infoAll($infoAll($i,3),8)           
            glVertex3f  $infoAll($infoAll($i,3),0) $infoAll($infoAll($i,3),1) $infoAll($infoAll($i,3),2)    
            }
        }   
         
    glEnd                            
}

proc DrawTetra0 {w} {

}
proc DrawTetra2 {w} {
    glShadeModel GL_SMOOTH 
    glBegin GL_LINES
#glEnable GL_DEPTH_TEST
    glEnable GL_LINE_SMOOTH 
#glLineWidth 10.0
    glEnable GL_MULTISAMPLE
    array set infoAll [Read_data]
    set points $infoAll(-1,0)
    set triangles $infoAll(-1,1)
    set flag $infoAll(-1,2)
        for {set i $points} {$i < [expr $points + $triangles]} {incr i} {
            if {$flag == 1} {
            #1�?
            glColor3f 0 0 0  
            #glColor3f $infoAll($infoAll($i,1),6) $infoAll($infoAll($i,1),7) $infoAll($infoAll($i,1),8) 
            glNormal3f $infoAll($infoAll($i,1),3) $infoAll($infoAll($i,1),4) $infoAll($infoAll($i,1),5)           
            glVertex3f  $infoAll($infoAll($i,1),0) $infoAll($infoAll($i,1),1) $infoAll($infoAll($i,1),2)               
            glColor3f 0 0 0  
            #glColor3f $infoAll($infoAll($i,2),6) $infoAll($infoAll($i,2),7) $infoAll($infoAll($i,2),8)   
            glNormal3f $infoAll($infoAll($i,2),3) $infoAll($infoAll($i,2),4) $infoAll($infoAll($i,2),5)             
            glVertex3f $infoAll($infoAll($i,2),0) $infoAll($infoAll($i,2),1) $infoAll($infoAll($i,2),2)             
            #3�?
            glColor3f 0 0 0  
            #glColor3f $infoAll($infoAll($i,3),6) $infoAll($infoAll($i,3),7) $infoAll($infoAll($i,3),8)  
            glNormal3f $infoAll($infoAll($i,3),3) $infoAll($infoAll($i,3),4) $infoAll($infoAll($i,3),5)          
            glVertex3f  $infoAll($infoAll($i,3),0) $infoAll($infoAll($i,3),1) $infoAll($infoAll($i,3),2)
            glColor3f 0 0 0  
            #glColor3f $infoAll($infoAll($i,1),6) $infoAll($infoAll($i,1),7) $infoAll($infoAll($i,1),8) 
            glNormal3f $infoAll($infoAll($i,1),3) $infoAll($infoAll($i,1),4) $infoAll($infoAll($i,1),5)           
            glVertex3f  $infoAll($infoAll($i,1),0) $infoAll($infoAll($i,1),1) $infoAll($infoAll($i,1),2)                
            #2,3
            glColor3f 0 0 0  
            #           glColor3f $infoAll($infoAll($i,2),6) $infoAll($infoAll($i,2),7) $infoAll($infoAll($i,2),8)   
            glNormal3f $infoAll($infoAll($i,2),3) $infoAll($infoAll($i,2),4) $infoAll($infoAll($i,2),5)             
            glVertex3f $infoAll($infoAll($i,2),0) $infoAll($infoAll($i,2),1) $infoAll($infoAll($i,2),2)
            glColor3f 0 0 0  
            #            glColor3f $infoAll($infoAll($i,3),6) $infoAll($infoAll($i,3),7) $infoAll($infoAll($i,3),8)  
            glNormal3f $infoAll($infoAll($i,3),3) $infoAll($infoAll($i,3),4) $infoAll($infoAll($i,3),5)          
            glVertex3f  $infoAll($infoAll($i,3),0) $infoAll($infoAll($i,3),1) $infoAll($infoAll($i,3),2) 
            } else {
            #1,2
            glColor3f 0 0 0  
            #           glColor3f $infoAll($infoAll($i,1),6) $infoAll($infoAll($i,1),7) $infoAll($infoAll($i,1),8)       
            glVertex3f  $infoAll($infoAll($i,1),0) $infoAll($infoAll($i,1),1) $infoAll($infoAll($i,1),2)               
            glColor3f 0 0 0  
            #           glColor3f $infoAll($infoAll($i,2),6) $infoAll($infoAll($i,2),7) $infoAll($infoAll($i,2),8)              
            glVertex3f $infoAll($infoAll($i,2),0) $infoAll($infoAll($i,2),1) $infoAll($infoAll($i,2),2) 
            #3,1            
            glColor3f 0 0 0  
            #           glColor3f $infoAll($infoAll($i,3),6) $infoAll($infoAll($i,3),7) $infoAll($infoAll($i,3),8)           
            glVertex3f  $infoAll($infoAll($i,3),0) $infoAll($infoAll($i,3),1) $infoAll($infoAll($i,3),2) 
            glColor3f 0 0 0  
            #            glColor3f $infoAll($infoAll($i,1),6) $infoAll($infoAll($i,1),7) $infoAll($infoAll($i,1),8)       
            glVertex3f  $infoAll($infoAll($i,1),0) $infoAll($infoAll($i,1),1) $infoAll($infoAll($i,1),2) 
            #2,3  
            glColor3f 0 0 0  
            #            glColor3f $infoAll($infoAll($i,2),6) $infoAll($infoAll($i,2),7) $infoAll($infoAll($i,2),8)              
            glVertex3f $infoAll($infoAll($i,2),0) $infoAll($infoAll($i,2),1) $infoAll($infoAll($i,2),2) 
            glColor3f 0 0 0  
            #glColor3f $infoAll($infoAll($i,3),6) $infoAll($infoAll($i,3),7) $infoAll($infoAll($i,3),8)           
            glVertex3f  $infoAll($infoAll($i,3),0) $infoAll($infoAll($i,3),1) $infoAll($infoAll($i,3),2) 
            }
        }        
    glEnd                               

    glBegin GL_TRIANGLES
    glEnable GL_BLEND
    glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA
    glBlendEquation GL_FUNC_ADD
    glEnable GL_POINT_SMOOTH
    glEnable GL_LINE_SMOOTH
    glEnable GL_POLYGON_SMOOTH

    array set infoAll [Read_data]
    set points $infoAll(-1,0)
    set triangles $infoAll(-1,1)
    set flag $infoAll(-1,2)
        for {set i $points} {$i < [expr $points + $triangles]} {incr i} {
            if {$flag == 1} {
            glColor3f $infoAll($infoAll($i,1),6) $infoAll($infoAll($i,1),7) $infoAll($infoAll($i,1),8) 
            glNormal3f $infoAll($infoAll($i,1),3) $infoAll($infoAll($i,1),4) $infoAll($infoAll($i,1),5)           
            glVertex3f  $infoAll($infoAll($i,1),0) $infoAll($infoAll($i,1),1) $infoAll($infoAll($i,1),2)               
            glColor3f $infoAll($infoAll($i,2),6) $infoAll($infoAll($i,2),7) $infoAll($infoAll($i,2),8)   
            glNormal3f $infoAll($infoAll($i,2),3) $infoAll($infoAll($i,2),4) $infoAll($infoAll($i,2),5)             
            glVertex3f $infoAll($infoAll($i,2),0) $infoAll($infoAll($i,2),1) $infoAll($infoAll($i,2),2)             
            glColor3f $infoAll($infoAll($i,3),6) $infoAll($infoAll($i,3),7) $infoAll($infoAll($i,3),8)  
            glNormal3f $infoAll($infoAll($i,3),3) $infoAll($infoAll($i,3),4) $infoAll($infoAll($i,3),5)          
            glVertex3f  $infoAll($infoAll($i,3),0) $infoAll($infoAll($i,3),1) $infoAll($infoAll($i,3),2)    
            } else {
            glColor3f $infoAll($infoAll($i,1),6) $infoAll($infoAll($i,1),7) $infoAll($infoAll($i,1),8)       
            glVertex3f  $infoAll($infoAll($i,1),0) $infoAll($infoAll($i,1),1) $infoAll($infoAll($i,1),2)               
            glColor3f $infoAll($infoAll($i,2),6) $infoAll($infoAll($i,2),7) $infoAll($infoAll($i,2),8)              
            glVertex3f $infoAll($infoAll($i,2),0) $infoAll($infoAll($i,2),1) $infoAll($infoAll($i,2),2)             
            glColor3f $infoAll($infoAll($i,3),6) $infoAll($infoAll($i,3),7) $infoAll($infoAll($i,3),8)           
            glVertex3f  $infoAll($infoAll($i,3),0) $infoAll($infoAll($i,3),1) $infoAll($infoAll($i,3),2)    
            }
        }   
         
    glEnd                            
}

proc Init { w } {
    global globalflag
    ## color set
    glClearColor 1.0 1.0 1.0 0.0
    glClear GL_COLOR_BUFFER_BIT
    if { [info exists ::sierList] } {
        glDeleteLists $::sierList 1
    }
    set ::sierList [glGenLists 1]
    glNewList $::sierList GL_COMPILE
    if {$globalflag == 0} {
        set ::buildTime [time {DrawTetra0 $w}]
    }
    if {$globalflag == 1} {
        set ::buildTime [time {DrawTetra $w}]
    }
    if {$globalflag == 2} {
        set ::buildTime [time {DrawTetra2 $w}]
    }
    glEndList
    $w postredisplay
    set ::buildInfo [format "time: %d msec to build" [expr [lindex $::buildTime 0] / 1000]]
}
proc Change1 {} {
    global globalflag
    set globalflag 1    
}
proc Change2 {} {
    global globalflag
    set globalflag 2
}

proc CreateCallback { w } {
    glClearColor 0 0 0 0
    glEnable GL_DEPTH_TEST
    glShadeModel GL_SMOOTH 
    Init $w
}

proc DisplayCallback { w } {
    glClear [expr $::GL_COLOR_BUFFER_BIT | $::GL_DEPTH_BUFFER_BIT] 
    glPushMatrix
    glTranslated 0 0 [expr -1.0 * $::vdist] 
    glRotatef $::xRotate 1.0 0.0 0.0 
    glRotatef $::yRotate 0.0 1.0 0.0
    glRotatef $::zRotate 0.0 0.0 1.0
    glCallList $::sierList
    glPopMatrix
    glLightfv GL_LIGHT0 GL_POSITION "0.0 0.0 -2.0 0.0"
    glLightfv GL_LIGHT0 GL_DIFFUSE "1.0 1.0 1.0 1.0"
    glLightfv GL_LIGHT0 GL_SPECULAR "0.0 0.0 0.0 1.0"
    glLightfv GL_LIGHT1 GL_POSITION  "0.0 0.0 2.0 0.0"
    glLightfv GL_LIGHT1 GL_DIFFUSE "1.0 1.0 1.0 1.0" 
    glLightfv GL_LIGHT1 GL_SPECULAR "0.0 0.0 0.0 1.0"
    glLightfv GL_LIGHT2 GL_POSITION "0.0 2.0 0.0 0.0"    
    glLightfv GL_LIGHT3 GL_POSITION "0.0 -2.0 0.0 0.0"    
    glLightfv GL_LIGHT4 GL_POSITION "2.0 0.0 0.0 0.0"   
    glLightfv GL_LIGHT5 GL_POSITION "-2.0 0.0 0.0 0.0"  
    glLightfv GL_LIGHT6 GL_POSITION "-1.0 10.0 -1.0 0.0"
    glLightfv GL_LIGHT7 GL_POSITION "1.0 -10.0 1.0 0.0"   
    glEnable GL_LIGHTING
    glEnable GL_LIGHT0
    glEnable GL_LIGHT1
    glEnable GL_LIGHT2
    glEnable GL_LIGHT3
    glEnable GL_LIGHT4
    glEnable GL_LIGHT5
    glEnable GL_LIGHT6
    glEnable GL_LIGHT7  
    glEnable GL_COLOR_MATERIAL
    $w swapbuffers
}

proc ReshapeCallback { toglwin { w -1 } { h -1 } } {
    set w [$toglwin width]
    set h [$toglwin height]
    glViewport 0 0 $w $h
    glMatrixMode GL_PROJECTION
    glLoadIdentity
    gluPerspective 60.0 [expr double($w)/double($h)] 1.0 2000.0
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    gluLookAt 0.0 0.0 5.0 0.0 0.0 0.0 0.0 1.0 0.0
}

proc Read_data {} {
    global trajFilesName
    set local [split $trajFilesName "/"]
    set len [llength $local]
    set location [lindex $local 0]
    for {set i 1} {$i < [expr $len - 1]} {incr i} {
        set location "$location/[lindex $local $i]"
    }

    set filename "$location/ses.ply"
    set content [open $filename r+]
    set row 0
    while {[gets $content line] >= 0} {     
        set list($row) $line
        set list($row) [string trim $list($row)]
        incr row
    }
    close $content
    set flag 0
    set flag2 0
    for {set i 0} {$i < $row} {incr i} {
        regsub -all {[[:blank:]]+} $list($i) "\t" newList
        set info [split $newList]
        for {set j 0} {$j < 10} {incr j} {
            set array($i,$j) [lindex $info $j]   
        }
        for {set j 0} {$j < 10} {incr j} {
        if {$array($i,$j) == "element"} {
            if {$array($i,[expr $j + 1]) == "vertex"} {
                set numPoints $array($i,[expr $j + 2])
            }
            if {$array($i,[expr $j + 1]) == "face"} {
                set numTriangles $array($i,[expr $j + 2])
            }
        }
        if {$array($i,$j) == "property"} {
            if {$array($i,[expr $j + 1]) == "uchar"} {
                set flag 1
            }
            if {$array($i,[expr $j + 2]) == "nx"} {
                set flag2 1
            } 
        }
        if {$array($i,0) == "end_header"} {
            set location [expr $i + 1]
        }
        }
    }
    for {set i 0} {$i < [expr $numPoints + $numTriangles]} {incr i} {
        for {set j 0} {$j < 10} {incr j} {
            set infoAll($i,$j)  $array([expr $i + $location],$j)  
        }
        if {$i < $numPoints} {
            if {$flag == 1} {
                for {set j 6} {$j < 9} {incr j} {
                    set infoAll($i,$j) [expr $infoAll($i,$j)/255.0]
                }
            } else {
                for {set j 6} {$j < 9} {incr j} {
                  set infoAll($i,$j) 0.5
                }
            }
        }
    }
    set infoAll(-1,0) $numPoints 
    set infoAll(-1,1) $numTriangles 
    set infoAll(-1,2) $flag2    
    set xSum 0
    set ySum 0
    set zSum 0
    for {set i 0} {$i < $numPoints} {incr i} {
        set xSum [expr $xSum + $infoAll($i,0)]
        set ySum [expr $ySum + $infoAll($i,1)]
        set zSum [expr $zSum + $infoAll($i,2)]
    }
    set xAvg [expr $xSum * 1.0 / $numPoints]
    set yAvg [expr $ySum * 1.0 / $numPoints]
    set zAvg [expr $zSum * 1.0 / $numPoints]
    for {set i 0} {$i < $numPoints} {incr i} {
        set infoAll($i,0) [expr $infoAll($i,0) - $xAvg] 
        set infoAll($i,1) [expr $infoAll($i,1) - $yAvg]
        set infoAll($i,2) [expr $infoAll($i,2) - $zAvg]
    }
    return [array get infoAll]

}

wm title . $appName 
set frMast [frame .fr]
set frTogl [frame .fr.togl]
set frBttn [frame .fr.bttn]
set frInfo [frame .fr.info]
set frInput [frame .fr.input]
set frResult [frame .fr.sesult]

pack $frMast -expand 1 -fill both
grid $frTogl -row 0 -column 0 -sticky news
grid $frBttn -row 1 -column 0 -sticky nws
grid $frResult -row 2 -column 0 -sticky nws 
grid $frInfo -row 3 -column 0 -sticky news
grid $frInput -row 0 -column 10 -sticky news
grid rowconfigure .fr 0 -weight 1
grid columnconfigure .fr 0 -weight 1

togl $frTogl.toglwin -width 500 -height 500 \
                     -double true -depth true \
                     -displayproc DisplayCallback \
                     -reshapeproc ReshapeCallback \
                     -createproc  CreateCallback
pack $frTogl.toglwin -side top -expand 1 -fill both







set input2 [frame $frInput.input2]
button $input2.b1 -text "Coor File" -command { trajfiles; setCoorType } -font $fontSize
label $input2.l1 -textvariable $trajShortFilesName -font $fontSize
button $input2.b2 -text "Receptor Coor" -command { retrajfiles; set trajflag1 1} -font $fontSize
label $input2.l2 -textvariable $reShortFilesName -font $fontSize
eval pack [winfo children $input2] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input2 -expand 1 -anchor w



set input1 [frame $frInput.input1]
button $input1.b1 -text "Top File" -command topfile -font $fontSize
label $input1.l1 -textvariable $topShortFileName -font $fontSize
button $input1.b2 -text "Ligand Coor" -command { litrajfiles; set trajflag2 1 } -font $fontSize
label $input1.l2 -textvariable $liShortFilesName -font $fontSize
eval pack [winfo children $input1] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input1 -expand 1 -anchor w

set input3 [frame $frInput.input3]
label $input3.l1 -text "Skip Init Data" -font $fontSize
entry $input3.l2 -width 4 -relief sunken -textvariable skipInitData -font $fontSize
label $input3.l3 -text "Skip Data" -font $fontSize
entry $input3.l4 -width 4 -relief sunken -textvariable skipData -font $fontSize
eval pack [winfo children $input3] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input3 -expand 1 -anchor w

set input18 [frame $frInput.input18]
ttk::separator $input18.s1 -orient horizontal 
eval pack [winfo children $input18] -padx 1 -pady 1 -side left -anchor w -expand 1 -fill x
pack $input18 -expand 1 -anchor w -fill x




set input7 [frame $frInput.input7]
label $input7.l1 -text "Continuum Model" -font $fontSize
radiobutton $input7.b1 -text PBSA -variable continuumModel -value PBSA -font $fontSize
radiobutton $input7.b2 -text GBSA -variable continuumModel -value GBSA -font $fontSize
eval pack [winfo children $input7] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input7 -expand 1 -anchor w


set input5 [frame $frInput.input5]
label $input5.l1 -text "Receptor Mols" -font $fontSize
checkbutton $input5.b1 -text 1 -variable molArray11 -command stateproc11 -font $fontSize
checkbutton $input5.b2 -text 2 -variable molArray12 -command stateproc12 -font $fontSize
checkbutton $input5.b3 -text 3 -variable molArray13 -command stateproc13 -font $fontSize
checkbutton $input5.b4 -text 4 -variable molArray14 -command stateproc14 -font $fontSize
eval pack [winfo children $input5] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input5 -expand 1 -anchor w

set input6 [frame $frInput.input6]
label $input6.l1 -text "Ligand Mols  " -font $fontSize
checkbutton $input6.b1 -text 1 -variable molArray21 -command stateproc21 -font $fontSize
checkbutton $input6.b2 -text 2 -variable molArray22 -command stateproc22 -font $fontSize
checkbutton $input6.b3 -text 3 -variable molArray23 -command stateproc23 -font $fontSize
checkbutton $input6.b4 -text 4 -variable molArray24 -command stateproc24 -font $fontSize
eval pack [winfo children $input6] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input6 -expand 1 -anchor w

set input10 [frame $frInput.input10]
label $input10.l1 -text "EPS In" -font $fontSize
entry $input10.l2 -width 4 -relief sunken -textvariable epsIn -font $fontSize
label $input10.l3 -text "EPS Out" -font $fontSize
entry $input10.l4 -width 4 -relief sunken -textvariable epsOut -font $fontSize
eval pack [winfo children $input10] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input10 -expand 1 -anchor w

proc stateproc11 {} {
    global molArray11 molArray21
    if {$molArray11 == 1} {
        set molArray21 0
    } 
}
proc stateproc21 {} {
    global molArray11 molArray21
    if {$molArray21 == 1} {
        set molArray11 0
    } 
}
proc stateproc12 {} {
    global molArray12 molArray22
    if {$molArray12 == 1} {
        set molArray22 0
    } 
}
proc stateproc22 {} {
    global molArray12 molArray22
    if {$molArray22 == 1} {
        set molArray12 0
    } 
}
proc stateproc13 {} {
    global molArray13 molArray23
    if {$molArray13 == 1} {
        set molArray23 0
    } 
}
proc stateproc23 {} {
    global molArray13 molArray23
    if {$molArray23 == 1} {
        set molArray13 0
    } 
}
proc stateproc14 {} {
    global molArray14 molArray24
    if {$molArray14 == 1} {
        set molArray24 0
    } 
}
proc stateproc24 {} {
    global molArray14 molArray24
    if {$molArray24 == 1} {
        set molArray14 0
    } 
}









set input9 [frame $frInput.input9]
label $input9.l1 -text "Salt Concentration" -font $fontSize
entry $input9.l2 -width 4 -relief sunken -textvariable saltCon -font $fontSize
label $input9.l3 -text "M/L" -font $fontSize
eval pack [winfo children $input9] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input9 -expand 1 -anchor w


set input31 [frame $frInput.input31]
label $input31.l4 -text "Temperature" -font $fontSize
entry $input31.l5 -width 6 -relief sunken -textvariable temperature -font $fontSize
label $input31.l6 -text "K" -font $fontSize
eval pack [winfo children $input31] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input31 -expand 1 -anchor w

set input19 [frame $frInput.input19]
ttk::separator $input19.s1 -orient horizontal 
eval pack [winfo children $input19] -padx 1 -pady 1 -side left -anchor w -expand 1 -fill x
pack $input19 -expand 1 -anchor w -fill x


set input11 [frame $frInput.input11]
label $input11.l1 -text "PB Eqn" -font $fontSize
radiobutton $input11.b1 -text linear -variable pb -value 1 -font $fontSize
radiobutton $input11.b2 -text nonlinear -variable pb -value 2 -font $fontSize
eval pack [winfo children $input11] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input11 -expand 1 -anchor w


set input8 [frame $frInput.input8]
label $input8.l1 -text "Grid H" -font $fontSize
entry $input8.l2 -width 4 -relief sunken -textvariable gridInterval -font $fontSize
label $input8.l3 -text "\u00C5" -font $fontSize
label $input8.l4 -text "  Probe Rad" -font $fontSize
entry $input8.l5 -width 4 -relief sunken -textvariable probeRad -font $fontSize
label $input8.l6 -text "\u00C5" -font $fontSize
eval pack [winfo children $input8] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input8 -expand 1 -anchor w


set input12 [frame $frInput.input12]
label $input12.l1 -text "Nonpolar" -font $fontSize
radiobutton $input12.b1 -text 1 -variable nonpolar -value 1 -font $fontSize
radiobutton $input12.b2 -text 2 -variable nonpolar -value 2 -font $fontSize
radiobutton $input12.b3 -text 3 -variable nonpolar -value 3 -font $fontSize
radiobutton $input12.b4 -text 4 -variable nonpolar -value 4 -font $fontSize
eval pack [winfo children $input12] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input12 -expand 1 -anchor w


set input32 [frame $frInput.input32]
ttk::separator $input32.s1 -orient horizontal 
eval pack [winfo children $input32] -padx 1 -pady 1 -side left -anchor w -expand 1 -fill x
pack $input32 -expand 1 -anchor w -fill x



set input13 [frame $frInput.input13]
label $input13.l1 -text "Entropy" -font $fontSize
radiobutton $input13.b1 -text no -variable entropy -value 0 -font $fontSize
radiobutton $input13.b2 -text yes -variable entropy -value 1 -font $fontSize
eval pack [winfo children $input13] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input13 -expand 1 -anchor w

set input14 [frame $frInput.input14]
label $input14.l1 -text "GB model" -font $fontSize
radiobutton $input14.b1 -text no -variable gb -value 0 -font $fontSize
radiobutton $input14.b2 -text yes -variable gb -value 1 -font $fontSize
eval pack [winfo children $input14] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input14 -expand 1 -anchor w

set input15 [frame $frInput.input15]
label $input15.l1 -text "Optimize" -font $fontSize
radiobutton $input15.b1 -text no -variable opt -value 0 -font $fontSize
radiobutton $input15.b2 -text yes -variable opt -value 1 -font $fontSize
eval pack [winfo children $input15] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input15 -expand 1 -anchor w 


set input20 [frame $frInput.input20]
ttk::separator $input20.s1 -orient horizontal 
eval pack [winfo children $input20] -padx 1 -pady 1 -side left -anchor w -expand 1 -fill x
pack $input20 -expand 1 -anchor w -fill x


set input17 [frame $frInput.input17]
#button $input17.run1 -text "Calculate" -command { calculate;outputtofile } -font $fontSize
button $input17.run1 -text "Calculate" -command { outputtofile; calculate } -font $fontSize
button $input17.run2 -text "Draw Surf" -command { Change1;Init $frTogl.toglwin;$frTogl.toglwin postredisplay } -font $fontSize
button $input17.run3 -text "Draw Mesh" -command { Change2;Init $frTogl.toglwin;$frTogl.toglwin postredisplay } -font $fontSize
eval pack [winfo children $input17] -padx 1 -pady 1 -side left -anchor w -expand 1
pack $input17 -expand 1 -anchor w


bind . <Key-Escape> { exit }
bind $frTogl.toglwin <1> {set cx %x; set cy %y}
bind $frTogl.toglwin <B1-Motion> {handleRot %x %y %W}
bind all <MouseWheel> {zoom $frTogl.toglwin %D}
bind all <Button-4> {event generate [focus -displayof %W] <MouseWheel> -delta  120}
bind all <Button-5> {event generate [focus -displayof %W] <MouseWheel> -delta -120}


proc zoom {w a} {
    global vdist
    set vdist [expr ($vdist - $a * 2 / 120)]
    $w postredisplay
}

proc setCoorType {} {
    global coor_type trajFilesName trajShortFilesName 
    set suffix [split_ $trajShortFilesName "."]
    set suffix [lindex $suffix 1]
    if {$suffix == "pqr"} {
        set coor_type 1
    } elseif {$suffix == "crd"} {
        set coor_type 2
    } elseif {$suffix == "nc" || $suffix == "ascii"} {
        set coor_type 3
    }
}


proc topfile {} {
    global topFileName topShortFileName top
    set filetype "{{file type:} {*.top}}";
    set titlename "Select the topfile"
    set filename [tk_getOpenFile  -filetypes $filetype -title $titlename]
    set topFileName $filename
    set local [split $filename "/"]
    set len [llength $local]



    set local [lindex $local [expr $len - 1]]
    set top $local
    set topShortFileName $local
}
proc trajfiles {} {
    global trajFilesName trajShortFilesName traj
    set filetype "{{file type:} { *.ascii *.pqr *.crd}}";
    set titlename "Select the trajfiles"
    set filename [tk_getOpenFile  -filetypes $filetype -title $titlename]
    set trajFilesName $filename
    set local [split $filename "/"]
    set len [llength $local]


    set location [lindex $local 0]
    for {set i 1} {$i < [expr $len - 1]} {incr i} {
        set location "$location/[lindex $local $i]"
    }
    cd "$location"

    set local [lindex $local [expr $len - 1]]
    set traj $local
    set trajShortFilesName $local
}
proc retrajfiles {} {
    global reFilesName reShortFilesName re 
    set reFilesName ""
    set filetype "{{file type:} { *.ascii *.pqr *.crd}}";
    set titlename "Select the threetrajfiles"
    set filename [tk_getOpenFile  -filetypes $filetype -title $titlename]
    set reFilesName $filename
    set local [split $filename "/"]
    set len [llength $local]
    set local [lindex $local [expr $len - 1]]
    set re $local
    set reShortFilesName $local
}
proc litrajfiles {} {
    global liFilesName liShortFilesName li
    set liFilesName ""
    set filetype "{{file type:} { *.ascii *.pqr *.crd}}";
    set titlename "Select the threetrajfiles"
    set filename [tk_getOpenFile  -filetypes $filetype -title $titlename]
    set liFilesName $filename
    set local [split $filename "/"]
    set len [llength $local]
    set local [lindex $local [expr $len - 1]]
    set li $local
    set liShortFilesName $local
}
proc split_ {str sep} {
    split [string map [list $sep \0] $str ] \0
}
proc calculate {} {
    global location1 trajFilesName
    set local [split $location1 "/"]
    set len [llength $local]
    set location [lindex $local 0]
    for {set i 1} {$i < [expr $len]} {incr i} {
        set location "$location/[lindex $local $i]"
    }

    set local2 [split $trajFilesName "/"]
    set len2 [llength $local2]
    set location2 [lindex $local2 0]
    for {set i 1} {$i < [expr $len2 - 1]} {incr i} {
        set location2 "$location2/[lindex $local2 $i]"
    }
    #puts $location
    #cd "$location"
    cd "$location2"
#    exec cmd.exe /c start $location/SurfPB.exe &
    exec bash job.sh
}
proc outputtofile {} {
    global skipInitData skipData threeTrajs molArray11 molArray12 molArray13 molArray14 molArray21 molArray22 molArray23 molArray24
    global continuumModel gridInterval probeRad saltCon temperature epsIn epsOut pb nonpolar entropy gb opt
    global topFileName trajFilesName mol1 mol2 coor_type 
    global trajflag1 trajflag2 reFilesName liFilesName
    set local [split $trajFilesName "/"]
    set len [llength $local]
    set location [lindex $local 0]
    for {set i 1} {$i < [expr $len - 1]} {incr i} {
        set location "$location/[lindex $local $i]"
    }

    set filename "$location/surfpb.in"
    set fp [open $filename w+]
    puts $fp "&surfpb"
    puts $fp "   isurface = 1"
    puts $fp "   topfile = \"$topFileName\""
    if { $coor_type == 1} {
        set coorFileName pqrfile
    }
    if { $coor_type == 2} {
        set coorFileName crdfile
    }
    if { $coor_type == 3} {
        set coorFileName trajfiles
    }
    
    if {$trajflag1 == 1 && $trajflag2 == 1 && $reFilesName ne "" && $liFilesName ne ""} {
        if {$reFilesName ne ""} {
            puts 1
        }
        if {$liFilesName eq ""} {
            puts 2
        }
        puts $fp "   iftripletrajs = .true."
        puts $fp "   $coorFileName = \"$trajFilesName\",\"$reFilesName\",\"$liFilesName\""
    } else {
        puts $fp "   $coorFileName = \"$trajFilesName\""
    }
    puts $fp "   saltcon = $saltCon"
    puts $fp "   epsin = $epsIn"
    puts $fp "   epsout = $epsOut"
    puts $fp "   ipbtype = $pb"
    puts $fp "   inonpolar = $nonpolar"
    puts $fp "   temperature = $temperature"
    puts $fp "   icuda = 0"
    puts $fp "   ientropy = $entropy"
    puts $fp "   igb = $gb"
    puts $fp "   imin = $opt"
    puts $fp "   skipinitdata = $skipInitData"
    puts $fp "   skipdata = $skipData"
    for {set i 0} {$i < 4} {incr i} {
        set left($i) 0
        set right($i) 0
    }
    set i 0;
    if {$molArray11 == 1} {
        incr i
        set left($i) 1
    }
    if {$molArray12 == 1} {
        incr i
        set left($i) 2
    }
    if {$molArray13 == 1} {
        incr i
        set left($i) 3
    }
    if {$molArray14 == 1} {
        incr i
        set left($i) 4
    }
    set j 0;
    if {$molArray21 == 1} {
        incr j
        set right($j) 1
    }
    if {$molArray22 == 1} {
        incr j
        set right($j) 2
    }
    if {$molArray23 == 1} {
        incr j
        set right($j) 3
    }
    if {$molArray24 == 1} {
        incr j
        set right($j) 4
    }   
    set mol1 ""
    for {set i 0} {$i < 4} {incr i} {
        if {$left($i) != 0} {
            set mol1 "$mol1$left($i),"
        }                                 
    } 
    set mol1 [string trimright $mol1 ,]
    set mol2 ""
    for {set i 0} {$i < 4} {incr i} {
        if {$right($i) != 0} {
            set mol2 "$mol2$right($i)," 
        }                                 
    }   
    set mol2 [string trimright $mol2 ,]                        
    puts $fp "   molarray = $mol1,0,$mol2"
    puts $fp "/"
    close $fp
}
